﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
using Tools;

namespace Punia
{
    [CommandAttribute(false)]
    public partial class service_permission : BaseForm
    {
        public service_permission()
            : base()
        {
        }

        public service_permission(DbContext dbContext)
            : base(dbContext)
        {
            InitializeComponent();

            cbPermission.DataSource = Utility.permissions(dbContext);
            cbPermission.DisplayMember = "Name";

            cbUser.DataSource = Utility.authorizations(dbContext);
            cbUser.DisplayMember = "Name";

        }

        protected override void Write()
        {
            StringBuilder sb = new StringBuilder();

            if (rbGrant.Checked)
            {
                sb.Append("GRANT ");
            }
            else
            {
                sb.Append("DENY ");
            }

            if (cbPermission.SelectedIndex > 0)
                sb.Append(cbPermission.Text + " ON ");
            else
                sb.Append("ON ");

            if (cbObject.SelectedIndex > 0)
                sb.Append(cbObject.Text.ToUpper() + "::");

            if (cbType.SelectedIndex > 0)
                sb.Append("[" + cbType.Text + "] TO ");
            else
                sb.Append(" TO ");

            if (cbUser.SelectedIndex > 0)
                sb.Append("[" + cbUser.Text + "]");
            else
                sb.Append("");

            tbCommand.Text = sb.ToString();
        }

        private void btnAdd_Click(object sender, EventArgs e)
        {
            Write();
        }

        private void cbPermission_SelectedIndexChanged(object sender, EventArgs e)
        {
            Write();
        }

        private void cbUser_SelectedIndexChanged(object sender, EventArgs e)
        {
            Write();
        }

        private void cbService_SelectedIndexChanged(object sender, EventArgs e)
        {
            Write();
        }

        private void cbObject_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (cbObject.SelectedIndex > 0)
            {
                if (cbObject.Text == "Service")
                {
                    cbType.DataSource = Utility.services(dbContext);
                    cbType.DisplayMember = "Name";
                }
                else if (cbObject.Text == "Queue")
                {
                    cbType.DataSource = Utility.queues(dbContext);
                    cbType.DisplayMember = "Name";
                }
                else if (cbObject.Text == "Endpoint")
                {
                    cbType.DataSource = Utility.endpoints(dbContext);
                    cbType.DisplayMember = "Name";
                }
            }
            else
            {
                cbType.DataSource = null;
            }

            Write();
        }

        private void rbGrant_CheckedChanged(object sender, EventArgs e)
        {
            Write();
        }

        private void rbDeny_CheckedChanged(object sender, EventArgs e)
        {
            Write();
        }
    }
}